package com.company.OfferGuide;

import com.company.ListNode;
import com.company.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class 从上到下打印二叉树 {

    public int[] levelOrder(TreeNode root) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            arrayList.add(node.val);

            if (node.left != null) queue.offer(node.left);
            if (node.right != null) queue.offer(node.right);
        }

        int[] data = new int[arrayList.size()];

        for (int i = 0; i < arrayList.size(); i++) {
            data[i] = arrayList.get(i);
        }
        return data;
    }

}
